package nieAS.display {
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import nieAS.utils.ObjectUtils;
	
	public class Button extends MovieClip {
		private var _btn:SimpleButton;
		protected var _canClick:Boolean = true;
		private var _isSprite = false;
		
		private static var buttonList:Vector.<Button> = new Vector.<Button>();
		
		/**
		 * 当前按钮ID(通过元件命名获得)
		 */
		protected var type:int = -1;
		protected var group:int = -1;
		
		public function Button():void {
			group = ObjectUtils.getObjectID(this.toString(), 2);
			type = ObjectUtils.getObjectID(this.toString(), 1);
			
			if (group != -1) {
				buttonList.push(this);
				this.addEventListener(Event.ADDED_TO_STAGE, function() {
					addToStage();
				});
			}
			
			_btn = this.getChildByName("btn_btn") as SimpleButton;
			if (_btn != null) {
				_isSprite = false;
				_btn.addEventListener(MouseEvent.MOUSE_OVER, function() {
						if (_canClick)
							toOver();
					});
				_btn.addEventListener(MouseEvent.MOUSE_OUT, function() {
						if (_canClick)
							toOut();
					});
				_btn.addEventListener(MouseEvent.CLICK, function() {
						if (_canClick)
							clickFunction();
					});
			} else {
				_isSprite = true;
				this.buttonMode = true;
				this.addEventListener(MouseEvent.MOUSE_OVER, function() {
						if (_canClick)
							toOver();
					});
				this.addEventListener(MouseEvent.MOUSE_OUT, function() {
						if (_canClick)
							toOut();
					});
				this.addEventListener(MouseEvent.CLICK, function() {
						if (_canClick)
							clickFunction();
					});
			}
		}
		
		protected function toOver():void {
			if (!_isSprite) {
				if (currentLabel != "over") {
					gotoAndPlay("over");
				}
			}else {
				TweenMax.to(this, 0.5, { colorTransform: { brightness:1.5 }} );
			}
		}
		
		protected function toOut():void {
			if (!_isSprite) {
				if (currentLabel != "out") {
					gotoAndPlay("out");
				}
			}else {
				TweenMax.to(this, 0.5, { colorTransform: { brightness:1 }} );
			}
		}
		
		protected function clickFunction():void {
		}
		
		protected static function setCurrent(_group:int):void {
			for (var i:int = 0; i < buttonList.length; i++) {
				if (buttonList[i].group == _group) {
					buttonList[i].addToStage();
				}
			}
		}
		
		protected function addToStage():void {
			if (!_canClick) {
				toOut();
				_canClick = true;
			}
		}
	}
}